{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.5ca1fb9ebb1188647ad7b06fe24ccac3.2592000.1591856876.282335-19841156\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "IOPub data rate exceeded.\n",
      "The notebook server will temporarily stop sending output\n",
      "to the client in order to avoid crashing it.\n",
      "To change this limit, set the config variable\n",
      "`--NotebookApp.iopub_data_rate_limit`.\n",
      "\n",
      "Current values:\n",
      "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n",
      "NotebookApp.rate_limit_window=3.0 (secs)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import base64 \n",
    "import requests \n",
    "def get_access_token(): \n",
    "    # 获取token的API \t\n",
    "    url = 'https://aip.baidubce.com/oauth/2.0/token' \n",
    "    # 获取access_token需要的参数 \n",
    "    params = { \n",
    "        # 固定参数 \n",
    "        'grant_type':'client_credentials', \n",
    "        # 必选参数，传入你的API Key \n",
    "        'client_id':'OxVVeFi1BIG26AEehvg3yOoW', \n",
    "        # 必选参数，传入你的Secret Key \n",
    "        'client_secret':'Sfny9w71UMOGDDsFbk77cxQAzcG5zZRa' \n",
    "    } \n",
    "    # 发送请求，获取响应数据 \n",
    "    response = requests.post(url, data=params) \n",
    "    # 将响应的数据转成字典类型，然后取出access_token \n",
    "    \n",
    "    access_token = eval(response.text)['access_token']\n",
    "    print(access_token)\n",
    "    # 将access_token返回 \n",
    "    return access_token \n",
    "def img2Cartoon(img): \n",
    "    # 头像动漫化的API \n",
    "    url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime' \n",
    "    \n",
    "    # 以二进制的方式读取原始图片 \n",
    "    origin_im = open(img, 'rb') \n",
    "    # 将图片进行base64编码 \n",
    "    img = base64.b64encode(origin_im .read()) \n",
    "    # 关闭原图片 \n",
    "    origin_im.close() \n",
    "    # 请求的headers信息，固定写法 \n",
    "    headers = {'content-type':'application/x-www-form-urlencoded'} \n",
    "    # 请求的参数 \n",
    "    params = { \n",
    "        # 开始获取的access_token \n",
    "        'access_token':get_access_token(), \n",
    "        # 图片的base64编码 \n",
    "        'image':img,\n",
    "    } \n",
    "    # 发送请求 \n",
    "    response = requests.post(url,data=params, headers=headers) \n",
    "    # 对响应结果进行处理 \n",
    "    if response:\n",
    "        # 打开一个文件 \n",
    "        f = open('result.jpg', 'wb') \n",
    "        # 获取动漫头像 \n",
    "        print(response.json())\n",
    "        anime = response.json()['image'] \n",
    "       \n",
    "        # 对返回的头像进行解码 \n",
    "        anime = base64.b64decode(anime) \n",
    "        # 将头像写入文件当中 \n",
    "        f.write(anime) \n",
    "        f.close() \n",
    "if __name__ == '__main__': \n",
    "    img2Cartoon('fujunj.jpg')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']\n"
     ]
    }
   ],
   "source": [
    "print(eva)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
